//==================================================================
//	Copyright (C) 2006  Davide Pasca
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program; if not, write to the Free Software
//	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//==================================================================
///
///
///
///
//==================================================================

#include "StdTypes.hlsl"

//==================================================================
uniform float4x4	_proj_obj_m		: register(c0);		
uniform float4x4	_view_obj_m		: register(c4);	
//uniform float4x4	_world_obj_m	: register(c8);	
uniform float3		_light_pos		: register(c12);	
uniform float4		_diffuse_col	: register(c13);
uniform float4		_emissive_col	: register(c14);
uniform float4x4	_r_light_obj_m	: register(c15);
uniform float4x4	_g_light_obj_m	: register(c19);
uniform float4x4	_b_light_obj_m	: register(c23);	
uniform float4		_fog_range		: register(c27);	// ( x, fog_end, (1/(fog_end-fog_start)), x)

//==================================================================
///
//==================================================================
VSOUT_PTC Std_Generic_VS(	const VSIN_PCTN input,
							bool has_tex, 
							bool has_col,
							bool has_nor )
{
	VSOUT_PTC  output = (VSOUT_PTC)0;

	output._pos = mul( _proj_obj_m, float4(input._pos, 1.0f) );

	if ( has_tex )
		output._tex = input._tex;

	if ( has_col )
		output._col = input._col.wxyz * _diffuse_col;
	else
		output._col = _diffuse_col;

	if ( has_nor )
	{
		float3	unit_view_norm = normalize( mul( (float3x3)_view_obj_m, input._nor ) );
//		float3	unit_world_norm = normalize( mul( (float3x3)_world_obj_m, input._nor ) );

		output._nor = unit_view_norm;
		output._viewdir = mul( _view_obj_m, float4(input._pos, 1.0f) );
		output._light_pos = _light_pos;

		float4	in_nor4 = float4( unit_view_norm, 1.0f );

		float4	tmp_r = mul( _r_light_obj_m, in_nor4 );
		float4	tmp_g = mul( _g_light_obj_m, in_nor4 );
		float4	tmp_b = mul( _b_light_obj_m, in_nor4 );

		// leave w (alpha) alone
		output._col.xyz = output._col.xyz * float3( dot( tmp_r, in_nor4 ),
													dot( tmp_g, in_nor4 ),
													dot( tmp_b, in_nor4 ) );
	
//		output._col.xyz *= float3( 0, 0, 0 );						// no color
//		output._col.xyz = (input._nor.xyz + float3(1,1,1)) * 0.5;	// normal color
	}
	else
		output._nor = float3( 1, 1, 1 );

	output._col.xyzw += _emissive_col;

    // fog calculation
    output._fog_coe.x = clamp( (_fog_range.y - output._pos.w) * _fog_range.z, 0, 1 );

/*	output._col = float4( has_tex ? 1 : 0.2f,
						  has_col ? 1 : 0.2f,
						  has_nor ? 1 : 0.2f,
						  1 );
*/

    return output;
}

//==================================================================
static const bool doTEX = true;
static const bool doCOL = true;
static const bool doNOR = true;

//==================================================================
VSOUT_PTC Std_P1_T0_C0_N0_VS( const VSIN_PCTN input )	{ return Std_Generic_VS( input, false, false, false ); }
VSOUT_PTC Std_P1_T0_C0_N1_VS( const VSIN_PCTN input )	{ return Std_Generic_VS( input, false, false, doNOR ); }
VSOUT_PTC Std_P1_T0_C1_N0_VS( const VSIN_PCTN input )	{ return Std_Generic_VS( input, false, doCOL, false ); }
VSOUT_PTC Std_P1_T0_C1_N1_VS( const VSIN_PCTN input )	{ return Std_Generic_VS( input, false, doCOL, doNOR ); }
VSOUT_PTC Std_P1_T1_C0_N0_VS( const VSIN_PCTN input )	{ return Std_Generic_VS( input, doTEX, false, false ); }
VSOUT_PTC Std_P1_T1_C0_N1_VS( const VSIN_PCTN input )	{ return Std_Generic_VS( input, doTEX, false, doNOR ); }
VSOUT_PTC Std_P1_T1_C1_N0_VS( const VSIN_PCTN input )	{ return Std_Generic_VS( input, doTEX, doCOL, false ); }
VSOUT_PTC Std_P1_T1_C1_N1_VS( const VSIN_PCTN input )	{ return Std_Generic_VS( input, doTEX, doCOL, doNOR ); }
